import { SiteStore } from '@/stores/site'
import rq from '@/utils/rq'
var model = {
  mounted() { },
  data() {
    return {

    }
  },
  computed: {

  },

  async mounted() {
    const Site = SiteStore()

    if (!Site.openid) {


      if (import.meta.env.MODE === 'development') {

        Site.openid = "oSNF2xM467WVbpVzUqHdKAG2m0tM";
        Site.aid = 1;
        Site.pid = 1;
        Site.project = { name: "测试项目", about: "测试项目描述" };
      }
      else {
        await this.loginuser();
      }



      if (typeof this.afterlogin === "function") {
        this.afterlogin();
      }

    }

    // if (Site.webinfo) {


    // }


    this.hascodelogin();

    //   Site.loading = false

  },

  methods: {


    async loginuser() {
      const Site = SiteStore()
      Site.aid = this.getUrlKey("aid");
      var pid = this.getUrlKey("pid");

      Site.pid = pid;


      var code = this.getUrlKey("code");

      window.history.pushState(null, null, '/index.php/autoqr/1/cash?pid=' + pid);


      var res = await this.$rq.post("wxpay", { op: "login", code: code, pid })



      if (res.status == 1) {

        Site.openid = res["user"]["openid"]
        Site.project = res["obj"]


      }





    },


    hascodelogin() {
      let url = window.location.href
      var code = this.getUrlKey("code");




    },

    getUrlKey(name, url = "") {
      if (!url) {
        url = window.location.href
      }
      return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(url) || [, ""])[1].replace(/\+/g, '%20')) || null

    }
  }
}
export default model
